//
//  StoreDetailView.m
//  HomePage
//
//  Created by xiong qi on 13-4-25.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "StoreDetailView.h"
#define AnimationDuration 0.4

@implementation StoreDetailView

- (id)initWithFrame:(CGRect)frame Data:(NSDictionary *)dic
{
    self = [super initWithFrame:frame];
    if (self) {
        
        textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 75, 506, 500)];
        [self addSubview:textView];
        [textView release];
        textView.editable = NO;
        textView.backgroundColor = [UIColor clearColor];
        [textView setFont:[UIFont fontWithName:@"Arial" size:16]];
        [textView setTextColor:[UIColor colorWithRed:0.545 green:0.545 blue:0.545 alpha:1.0]];
        textView.text = [dic objectForKey:@"des"];
        
        
        scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(625, 75, 350, 500)];
        [self addSubview:scrollview];
        [scrollview release];
        
        NSArray * array = (NSArray *)[dic objectForKey:@"pic"];
        [self LoadAllImages:array];
        

    }
    return self;
}

-(void)LoadAllImages:(NSArray *)array
{
    int count = [array count];
    int spacetop = 20;
    
    UIImage *img=[UIImage imageNamed:@"AllAuto_Comparison_Image_BG"];
    img=[img stretchableImageWithLeftCapWidth:15 topCapHeight:12];
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * localpath= [NSString stringWithFormat:@"%@/paddata",[paths objectAtIndex:0]];
    
    TouchImageView * tempimageview;
    UIImageView * imageView;
    NSString * path;
    
    for (int i=0; i<count; i++) {
        tempimageview = [[TouchImageView alloc] initWithFrame:CGRectMake(0, (spacetop+240)*i, 350, 240)];
        [tempimageview SetImage:img];
        tempimageview.Type = 1;
        [scrollview addSubview:tempimageview];
        [tempimageview release];
        tempimageview.delegate = self;
        
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(2,2, tempimageview.frame.size.width-4, tempimageview.frame.size.height-4)];
        [imageView setImage:img];
        [tempimageview addSubview:imageView];
        [imageView release];

        path = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:i]];
        
        UIImage * image = [[UIImage alloc] initWithContentsOfFile:path];
        imageView.image = image;
        [image release];
        tempimageview.dataSource = path;
    }
    
    scrollview.contentSize = CGSizeMake(scrollview.frame.size.width, (spacetop+240)*count);
}

-(void)LoadAllVideos:(NSArray *)array
{
    int count = [array count];
    int top = scrollview.contentSize.height;
    int spacetop = 20;
    
    UIImage *img=[UIImage imageNamed:@"AllAuto_Comparison_Image_BG"];
    img=[img stretchableImageWithLeftCapWidth:15 topCapHeight:12];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * localpath= [NSString stringWithFormat:@"%@/paddata",[paths objectAtIndex:0]];
    
    TouchImageView * tempimageview;
    MoviePlayView * movieView;
    NSString * path;
    
    for (int i=0; i<count; i++) {
        tempimageview = [[TouchImageView alloc] initWithFrame:CGRectMake(0, top + (spacetop+240)*i, 350, 240)];
        [tempimageview SetImage:img];
        tempimageview.Type = 2;
        [scrollview addSubview:tempimageview];
        [tempimageview release];
        tempimageview.delegate = self;
        
        
        movieView = [[MoviePlayView alloc] initWithFrame:CGRectMake(2,2, tempimageview.frame.size.width-4, tempimageview.frame.size.height-4)];
        [tempimageview addSubview:movieView];
        [movieView release];
        movieView.userInteractionEnabled = NO;
        
        path = [NSString stringWithFormat:@"%@/local/%@",localpath,[array objectAtIndex:i]];
        [movieView SetPlayPath:path];
        tempimageview.dataSource = path;
    }
    
    scrollview.contentSize = CGSizeMake(scrollview.frame.size.width,top + (spacetop+240)*count);
}

-(void)TouchImageViewBeTouch:(TouchImageView *)view
{
    
    if (mediaview == nil) {
        mediaview = [[MediaView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    }
    mediaview.alpha = 0;
    
    if (view.Type == 1) {
        [mediaview Refresh:(NSString *)view.dataSource Type:0];
    }
    else if(view.Type == 2)
    {
        [mediaview Refresh:(NSString *)view.dataSource Type:1];
    }
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:mediaview];
    
    [self ShowMediaView];
    
}

-(void)ShowMediaView
{
    [UIView animateWithDuration:AnimationDuration
                     animations:^{
                         
                         mediaview.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         
                         
                     }];
}

-(void)dealloc
{
    SafeRelease(mediaview);
    [super dealloc];
}

@end
